package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

type EditUserInfoType struct {
	Id       int    `form:"id"`
	Account  string `form:"account"`
	Password string `form:"password"`
}
type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func main() {

	//---------------request----------------------
	//---------------request----------------------
	//---------------request----------------------
	engine := gin.Default()
	engine.GET("/hello", func(c *gin.Context) {
		/*c.JSON(200,gin.H{
			"message":"hello world",
		})*/
		_, _ = c.Writer.Write([]byte("hello world"))

	})
	engine.Handle("GET", "/getUserInfo", func(context *gin.Context) {
		_, _ = context.Writer.WriteString("获取用户信息")
	})

	//接收表单参数
	engine.POST("/register", func(c *gin.Context) {
		account, _ := c.GetPostForm("account")
		password, _ := c.GetPostForm("password")
		code, _ := c.GetPostForm("code")
		_, _ = c.Writer.WriteString(account + password + code)
	})

	// 接收json方式的数据
	engine.PUT("/editUserInfo", func(c *gin.Context) {
		var editUserInfoType EditUserInfoType
		if err := c.ShouldBindJSON(&editUserInfoType); err != nil {
			fmt.Println(err)
		}
		fmt.Println(editUserInfoType.Id)
		fmt.Println(editUserInfoType.Account)
		fmt.Println(editUserInfoType.Password)
		//_, _ = c.Writer.WriteString(id + account + password)
	})

	engine.DELETE("/delete/:id", func(c *gin.Context) {
		id := c.Param("id")
		value, _ := strconv.Atoi(id)
		fmt.Println(value)
		fmt.Printf("%T\n", id)
		fmt.Printf("%T\n", value)

	})

	//---------------response----------------------
	//---------------response----------------------
	//---------------response----------------------
	engine.GET("/resGet", func(context *gin.Context) {
		_, _ = context.Writer.WriteString("12312313")
	})
	engine.GET("/resJson", func(context *gin.Context) {
		context.JSON(200, map[string]interface{}{
			"code":    1,
			"message": "success",
			"data":    nil,
		})
	})
	engine.GET("resJson1", func(context *gin.Context) {
		result := Response{Code: 1, Msg: "success", Data: nil}
		context.JSON(200, result)
	})

	if err := engine.Run(":8092"); err != nil {
		fmt.Println("gin run error")
	}

}
